/*================================================================
*   Copyright (C) 2019 Guangzhou Firefly Ltd. All rights reserved.
*   
*   文件名称：led_alert.c
*   创 建 者：luhuadong
*   创建日期：2019年08月30日
*   描    述：
*
================================================================*/


#include "led_alert.h"


static void led_entry(void *parameter)
{
	rt_uint32_t e;

	while(1)
	{
		/* 接收事件 */
	if (RT_EOK == rt_event_recv(&led_event,                             /* 事件集对象句柄 */
		          (EVENT_LED0_ON | EVENT_LED0_OFF | EVENT_LED0_BLINK),  /* 接收事件 */
		          RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,               /* 接收选项 */
		          RT_WAITING_FOREVER,                                   /* 超时时间 */
		          &e))                                                  /* 指向收到的事件 */
    {
        rt_kprintf("led_entry: OR recv event 0x%x\n", e);
    }

	/* 判断标志位 */
	if(e & EVENT_LED0_ON)
	{
		led_on(LED0_PIN);
	}
	if(e & EVENT_LED0_ON)
	{
		led_off(LED0_PIN);
	}
	if(e & EVENT_LED0_ON)
	{
		led_blink(LED0_PIN);
		led_blink(LED0_PIN);
	}


	}

}

int led_init()
{
	rt_thread_t led_thread;
	rt_err_t result;

	rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
	rt_pin_write(LED0_PIN, PIN_LOW);

	/* 初始化事件对象 */
    result = rt_event_init(&led_event, "event", RT_IPC_FLAG_FIFO);
    if (result != RT_EOK)
    {
        rt_kprintf("init event failed.\n");
        return -1;
    }

    /* 创建线程 */
    led_thread = rt_thread_create("led_th",
                                   led_entry,
                                   RT_NULL,
                                   1024,
                                   RT_THREAD_PRIORITY_MAX / 2 + 1,
                                   20);
    if (led_thread != RT_NULL)
    {
        rt_thread_startup(led_thread);
    }

    return RT_EOK;
}


void led_on(rt_base_t led_pin)
{
	rt_pin_write(led_pin, PIN_HIGH);
}


void led_off(rt_base_t led_pin)
{
	rt_pin_write(led_pin, PIN_LOW);
}


void led_blink_time(rt_int32_t ms)
{
	rt_pin_write(LED0_PIN, PIN_HIGH);
	rt_thread_mdelay(ms);
	rt_pin_write(LED0_PIN, PIN_LOW);
	rt_thread_mdelay(ms);
}

void led_blink(rt_base_t led_pin)
{
	rt_pin_write(led_pin, PIN_HIGH);
	rt_thread_mdelay(500);
	rt_pin_write(led_pin, PIN_LOW);
	rt_thread_mdelay(500);
}



